package com.qianxx.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.qianxx.base.R;


/**
 * 使用 Checked 的实现直接使用 selected 属性的功能 <p/>
 * <p>
 * checked: true -> selected: true <br>
 * checked: false -> selected: false <br>
 * <p>
 * <p>
 * Created on 16/3/11 下午3:36.
 *
 * @author binwin20
 */
public class ExCheckBox extends ImageView implements View.OnClickListener {

    private OnClickListener mOnClickListener;

    public ExCheckBox(Context context) {
        this(context, null);
    }

    public ExCheckBox(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ExCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ExCheckBox);

        boolean checked = a.getBoolean(R.styleable.ExCheckBox_checked, false);
        setSelected(checked);
        a.recycle();
        super.setOnClickListener(this);
    }

    private void changeChecked() {
        setSelected(!isSelected());

    }

    public boolean isChecked() {
        return isSelected();
    }

    @Override
    public void onClick(View v) {
        changeChecked();
        if (mOnClickListener != null) {
            mOnClickListener.onClick(v);
        }
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        // super.setOnClickListener(l);
        mOnClickListener = l;
    }
}
